package com.lxmes.business.service.impl;

import java.util.List;

import com.lxmes.business.domain.CcOrderIteminfo;
import com.lxmes.business.mapper.CcOrderIteminfoMapper;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.business.mapper.CcOrderInfoMapper;
import com.lxmes.business.domain.CcOrderInfo;
import com.lxmes.business.service.ICcOrderInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-27
 */
@Service
public class CcOrderInfoServiceImpl implements ICcOrderInfoService 
{
    @Autowired
    private CcOrderInfoMapper ccOrderInfoMapper;

    @Autowired
    private CcOrderIteminfoMapper ccOrderIteminfoMapper;

    /**
     * 查询订单信息
     * 
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public CcOrderInfo selectCcOrderInfoById(Long id)
    {
        CcOrderInfo ccOrderInfo = ccOrderInfoMapper.selectCcOrderInfoById(id);
        CcOrderIteminfo searchOrDerInfo = new CcOrderIteminfo();
        searchOrDerInfo.setOrderId(ccOrderInfo.getId());
        List<CcOrderIteminfo> ccOrderIteminfoList = ccOrderIteminfoMapper.selectCcOrderIteminfoList(searchOrDerInfo);
        ccOrderInfo.setOrderIteminfoList(ccOrderIteminfoList);
        return ccOrderInfo;
    }

    /**
     * 查询订单信息列表
     * 
     * @param ccOrderInfo 订单信息
     * @return 订单信息
     */
    @Override
    public List<CcOrderInfo> selectCcOrderInfoList(CcOrderInfo ccOrderInfo)
    {
        return ccOrderInfoMapper.selectCcOrderInfoList(ccOrderInfo);
    }

    /**
     * 新增订单信息
     * 
     * @param ccOrderInfo 订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCcOrderInfo(CcOrderInfo ccOrderInfo)
    {
        ccOrderInfo.setCreateTime(DateUtils.getNowDate());
        int i = ccOrderInfoMapper.insertCcOrderInfo(ccOrderInfo);
        ccOrderInfo.setJzFlag("未结清");
        List<CcOrderIteminfo> orderIteminfoList = ccOrderInfo.getOrderIteminfoList();
        if(orderIteminfoList!=null&&orderIteminfoList.size()>0){
            for (CcOrderIteminfo ccOrderIteminfo : orderIteminfoList) {
                ccOrderIteminfo.setOrderId((long) i);
                ccOrderIteminfoMapper.insertCcOrderIteminfo(ccOrderIteminfo);
            }
        }

        return i;
    }

    /**
     * 修改订单信息
     * 
     * @param ccOrderInfo 订单信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCcOrderInfo(CcOrderInfo ccOrderInfo)
    {
        ccOrderInfo.setUpdateTime(DateUtils.getNowDate());
        List<CcOrderIteminfo> orderIteminfoList = ccOrderInfo.getOrderIteminfoList();
        if(orderIteminfoList!=null&&orderIteminfoList.size()>0){
            ccOrderIteminfoMapper.deleteCcOrderIteminfoByOrderId(ccOrderInfo.getId());
            for (CcOrderIteminfo ccOrderIteminfo : orderIteminfoList) {
                ccOrderIteminfo.setOrderId(ccOrderInfo.getId());
                ccOrderIteminfoMapper.insertCcOrderIteminfo(ccOrderIteminfo);
            }
        }
        return ccOrderInfoMapper.updateCcOrderInfo(ccOrderInfo);
    }

    /**
     * 批量删除订单信息
     * 
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteCcOrderInfoByIds(Long[] ids)
    {
        for (Long id : ids) {
            List<CcOrderIteminfo> ccOrderIteminfoList = ccOrderIteminfoMapper.selectCcOrderIteminfoByorderId(id);
            if(ccOrderIteminfoList!=null){
                throw new ServiceException("订单详情有数据无法删除");
            }
        }
        return ccOrderInfoMapper.deleteCcOrderInfoByIds(ids);
    }

    /**
     * 删除订单信息信息
     * 
     * @param id 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteCcOrderInfoById(Long id)
    {
        List<CcOrderIteminfo> ccOrderIteminfoList = ccOrderIteminfoMapper.selectCcOrderIteminfoByorderId(id);
        if(ccOrderIteminfoList!=null){
            throw new ServiceException("订单详情有数据无法删除");
        }
        return ccOrderInfoMapper.deleteCcOrderInfoById(id);
    }
}
